package com.sas.repository;

import com.sas.entity.SalaryInfo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

/**
 * 薪资信息Repository接口
 */
@Repository
public interface SalaryInfoRepository extends JpaRepository<SalaryInfo, Long> {

    /**
     * 根据薪资等级和员工类型查询有效的薪资信息
     */
    @Query("SELECT s FROM SalaryInfo s WHERE s.salaryLevel = :salaryLevel AND s.employeeType = :employeeType " +
           "AND s.status = 1 AND s.effectiveDate <= :currentDate " +
           "AND (s.expiryDate IS NULL OR s.expiryDate > :currentDate)")
    Optional<SalaryInfo> findEffectiveSalaryInfo(@Param("salaryLevel") Integer salaryLevel, 
                                                 @Param("employeeType") Integer employeeType,
                                                 @Param("currentDate") LocalDateTime currentDate);

    /**
     * 根据员工类型查询所有有效薪资信息
     */
    @Query("SELECT s FROM SalaryInfo s WHERE s.employeeType = :employeeType AND s.status = 1 " +
           "AND s.effectiveDate <= :currentDate " +
           "AND (s.expiryDate IS NULL OR s.expiryDate > :currentDate)")
    List<SalaryInfo> findByEmployeeTypeAndEffective(@Param("employeeType") Integer employeeType,
                                                    @Param("currentDate") LocalDateTime currentDate);

    /**
     * 查询所有有效的薪资信息
     */
    @Query("SELECT s FROM SalaryInfo s WHERE s.status = 1 " +
           "AND s.effectiveDate <= :currentDate " +
           "AND (s.expiryDate IS NULL OR s.expiryDate > :currentDate)")
    List<SalaryInfo> findAllEffective(@Param("currentDate") LocalDateTime currentDate);

    /**
     * 根据薪资等级查询薪资信息
     */
    List<SalaryInfo> findBySalaryLevel(Integer salaryLevel);

    /**
     * 根据员工类型查询薪资信息
     */
    List<SalaryInfo> findByEmployeeType(Integer employeeType);
}